package com.lanou.zakerproject.splash;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;
import android.widget.TextView;
import com.lanou.zakerproject.R;
import com.lanou.zakerproject.base.BaseActivity;
import com.lanou.zakerproject.subscribe.MainActivity;
import android.os.Handler;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by dllo on 16/1/13.
 */
public class SplashActivity extends BaseActivity implements View.OnClickListener {
    private ImageView imageView;
    private TextView tv_first, tv_second;
    private Handler handler;
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            isIntent();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        tv_first = (TextView) findViewById(R.id.tv_splash_first);
        tv_second = (TextView) findViewById(R.id.tv_splash_second);
        imageView = (ImageView) findViewById(R.id.iv_splash);
        handler = new Handler();
        handler.postDelayed(runnable, 5000);
        animotionStart();
        imageView.setOnClickListener(this);//加监听
    }


    //动画播放
    private void animotionStart() {
        //图片
        AnimationSet set = new AnimationSet(true);
        AlphaAnimation alphaAnimation = new AlphaAnimation(1f, 0f);
        alphaAnimation.setDuration(5000);
        set.addAnimation(alphaAnimation);
        imageView.startAnimation(set);

        //字
        ObjectAnimator animator1 = new ObjectAnimator().ofFloat(tv_first, "translationY", 0f, 1100f);
        animator1.setDuration(8000).setStartDelay(500);
        animator1.setInterpolator(new BounceInterpolator());//弹跳效果
        animator1.start();
        animator1.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                tv_first.setVisibility(View.GONE);
            }
        });
        ObjectAnimator animator2 = new ObjectAnimator().ofFloat(tv_second, "translationY", 0f, 1000f);
        animator2.setDuration(5000);
        animator2.setInterpolator(new BounceInterpolator());
        animator2.start();
        animator2.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                tv_second.setVisibility(View.GONE);
            }
        });
    }
    //跳转
    private void isIntent() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        this.finish();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_splash:
                isIntent();//点击图片立即跳转
                handler.removeCallbacks(runnable);//移除 handler
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }
}
